/**
 * Created by 功振 on 2016/1/30.
 */
angular.module('NatureCloud').controller('BuildDetailController', ["$scope", "$stateParams", "builderService","$document","$q", function ($scope, $stateParams, builderService,$document,$q) {
    var jobName = $stateParams.jobName;
    $scope.jobName = jobName;
    var buildSeq = $stateParams.buildSeq;
    $scope.buildSeq = buildSeq;
    var start = 0;
    $scope.havaData = true;

    $scope.templateUrl = '/template/cp/ngbusytemplate.html';
    var spin = setTimeout(function () {
    var _spin = angular.element(".spinner")
        .fadeIn("fast")
        .delay(1000000);
    },200);

    var testPromise = $q.defer();
    $scope.myPromise = testPromise.promise;

    builderService.getBuildConOut(jobName, buildSeq,start).then(function (response) {

        var lines = response.data;
        var test = [];
        for (var i = 0; i < lines.length / 60; i ++){
            var testArrOne = lines.slice(i * 60, i * 60 + 60);
            test[i] = testArrOne;
        }

        var infos = [];
        for(var i = 0; i < test.length; i++){
            var string = JSON.stringify(test[i].join('\n'));
            infos[i] = JSON.parse(string);
        }


        $scope.line_number = test.length;

        $scope.currentPageRunInfos = [];
        $scope.currentPage = 1;
        $scope.numPerPage = 1;
        $scope.$watch("lines + currentPage", function() {
            if (lines) {
                var begin = (($scope.currentPage - 1) * $scope.numPerPage)
                    , end = begin + $scope.numPerPage;
                $scope.currentPageRunInfos = infos.slice(begin, end);
                $scope.havaData = false;
                testPromise.resolve();
                clearTimeout(spin);
            }
        });

    });

    function replaceTwo(scrstr,armstr) {

        while(scrstr.indexOf(armstr) >= 1)
        {
            scrstr = scrstr.replace(armstr,"\\n");
        }
        return scrstr;
    }

    $scope.scrollBottom = function () {
      var pageDiv = angular.element(document.getElementById("page"));
        jQuery(".main-content").scrollToElement(pageDiv,0,1500);
    };


}]);
